package com.yw.wanandroid

import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.databinding.BindingAdapter
import com.bumptech.glide.Glide

@BindingAdapter("isGone")
fun bindIsGone(view: View, isGone: Boolean) {
    view.visibility = if (isGone) {
        View.GONE
    } else {
        View.VISIBLE
    }
}

@BindingAdapter("isHide")
fun bindIsHide(view: View, isHide: Boolean) {
    view.visibility = if (isHide) {
        View.INVISIBLE
    } else {
        View.VISIBLE
    }
}

@BindingAdapter("imageFromUrl")
fun bindImageFromUrl(view: ImageView, imageUrl: String?) {
    if (!imageUrl.isNullOrEmpty()) {
        Glide.with(view.context)
            .load(imageUrl)
            .into(view)
    }
}

@BindingAdapter("bindHomeArticleImgUrl")
fun bindHomeArticleImgUrl(view: ImageView, imageUrl: String?) {
    if (!imageUrl.isNullOrEmpty()) {
        view.visibility = View.VISIBLE
        Glide.with(view.context)
            .load(imageUrl)
            .into(view)
    } else {
        view.visibility = View.GONE
    }
}

@BindingAdapter("bindHomeArticleDesc")
fun bindHomeArticleDesc(view: TextView, str: String?) {
    if (!str.isNullOrEmpty()) {
        view.visibility = View.VISIBLE
        view.text = str
    } else {
        view.visibility = View.GONE
    }
}

